
using Nop.Core.Caching;
using Nop.Core.Data;
using Nop.Core.Domain.XJ;
using Nop.Services.Events;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Nop.Services.XJ
{
	/// <summary>
	/// XJMaintenanceCustomers service
	/// </summary>
	public partial class XJMaintenanceCustomersService : IXJMaintenanceCustomersService
	{
		#region Constants

		/// <summary>
		/// Key for caching
		/// </summary>
		private const string XJMAINTENANCECUSTOMERSS_ALL_KEY = "Nop.xjmaintenancecustomers.all";
		/// <summary>
		/// Key for caching
		/// </summary>
		/// <remarks>
		/// {0} : xJMaintenanceCustomers ID
		/// </remarks>
		private const string XJMAINTENANCECUSTOMERS_BY_ID_KEY = "Nop.xjmaintenancecustomers.id-{0}";
		/// <summary>
		/// Key pattern to clear cache
		/// </summary>
		private const string XJMAINTENANCECUSTOMERSS_PATTERN_KEY = "Nop.xjmaintenancecustomers.";
		#endregion

		#region Fields
		private readonly IRepository<XJMaintenanceCustomers> _xJMaintenanceCustomersRepository;
		private readonly IEventPublisher _eventPublisher;
		private readonly ICacheManager _cacheManager;
		#endregion

		#region Ctor

		/// <summary>
		/// Ctor
		/// </summary>
		/// <param name="cacheManager">Cache manager</param>
		/// <param name="xJMaintenanceCustomersRepository">XJMaintenanceCustomers repository</param>
		/// <param name="eventPublisher">Event published</param>
		public XJMaintenanceCustomersService(ICacheManager cacheManager,
			IRepository<XJMaintenanceCustomers> xJMaintenanceCustomersRepository,
			IEventPublisher eventPublisher)
		{
			this._cacheManager = cacheManager;
			this._xJMaintenanceCustomersRepository = xJMaintenanceCustomersRepository;
			this._eventPublisher = eventPublisher;
		}

		#endregion

		#region Methods

		/// <summary>
		/// Gets all XJMaintenanceCustomerss
		/// </summary>
		/// <returns>XJMaintenanceCustomerss</returns>
		public virtual IList<XJMaintenanceCustomers> GetAllXJMaintenanceCustomerss()
		{
			string key = XJMAINTENANCECUSTOMERSS_ALL_KEY;
			return _cacheManager.Get(key, () =>
			{
				var query = from a in _xJMaintenanceCustomersRepository.Table
							orderby a.Id
							select a;
				return query.ToList();
			});
		}

		public virtual IList<XJMaintenanceCustomers> GetMaintenanceCustomerss(int maintenanceId)
		{
			var query = from a in _xJMaintenanceCustomersRepository.Table
						where a.MaintenanceId == maintenanceId
						orderby a.Id
						select a;
			return query.ToList();
		}

		/// <summary>
		/// Gets a XJMaintenanceCustomers 
		/// </summary>
		/// <param name="xJMaintenanceCustomersId">XJMaintenanceCustomers identifier</param>
		/// <returns>XJMaintenanceCustomers</returns>
		public virtual XJMaintenanceCustomers GetXJMaintenanceCustomersById(int xJMaintenanceCustomersId)
		{
			if (xJMaintenanceCustomersId == 0)
				return null;

			string key = string.Format(XJMAINTENANCECUSTOMERS_BY_ID_KEY, xJMaintenanceCustomersId);
			return _cacheManager.Get(key, () => _xJMaintenanceCustomersRepository.GetById(xJMaintenanceCustomersId));
		}

		/// <summary>
		/// Inserts a XJMaintenanceCustomers
		/// </summary>
		/// <param name="xJMaintenanceCustomers">XJMaintenanceCustomers</param>
		public virtual void InsertXJMaintenanceCustomers(XJMaintenanceCustomers xJMaintenanceCustomers)
		{
			if (xJMaintenanceCustomers == null)
				throw new ArgumentNullException("xJMaintenanceCustomers");

			_xJMaintenanceCustomersRepository.Insert(xJMaintenanceCustomers);

			_cacheManager.RemoveByPattern(XJMAINTENANCECUSTOMERSS_PATTERN_KEY);

			//event notification
			_eventPublisher.EntityInserted(xJMaintenanceCustomers);
		}

		/// <summary>
		/// Updates the XJMaintenanceCustomers
		/// </summary>
		/// <param name="xJMaintenanceCustomers">XJMaintenanceCustomers</param>
		public virtual void UpdateXJMaintenanceCustomers(XJMaintenanceCustomers xJMaintenanceCustomers)
		{
			if (xJMaintenanceCustomers == null)
				throw new ArgumentNullException("xJMaintenanceCustomers");

			_xJMaintenanceCustomersRepository.Update(xJMaintenanceCustomers);

			_cacheManager.RemoveByPattern(XJMAINTENANCECUSTOMERSS_PATTERN_KEY);

			//event notification
			_eventPublisher.EntityUpdated(xJMaintenanceCustomers);
		}

		/// <summary>
		/// Deletes a XJMaintenanceCustomers
		/// </summary>
		/// <param name="xJMaintenanceCustomers">XJMaintenanceCustomers</param>
		public virtual void DeleteXJMaintenanceCustomers(XJMaintenanceCustomers xJMaintenanceCustomers)
		{
			if (xJMaintenanceCustomers == null)
				throw new ArgumentNullException("xJMaintenanceCustomers");

			_xJMaintenanceCustomersRepository.Delete(xJMaintenanceCustomers);

			_cacheManager.RemoveByPattern(XJMAINTENANCECUSTOMERSS_PATTERN_KEY);

			//event notification
			_eventPublisher.EntityDeleted(xJMaintenanceCustomers);
		}

		#endregion
	}
}
